import colorsys
import PIL.Image as Image

def cut_image(image_path):
    img = Image.open(image_path)
    # [274,849][416,890]
    crop = img.crop((274, 794, 494, 835))
    rz_path = r'C:\Users\ZGS\Desktop\herhome\验证appium\tb\rz.png'
    crop.save(rz_path)
    image = Image.open(rz_path)
    image = image.convert('RGB')
    print(get_dominant_color(image))

def get_dominant_color(image):
    max_score = 0.0001
    dominant_color = None
    for count, (r, g, b) in image.getcolors(image.size[0] * image.size[1]):
        # 转为HSV标准
        saturation = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)[1]
        y = min(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235)
        y = (y - 16.0) / (235 - 16)

        # 忽略高亮色
        if y > 0.9:
            continue
        score = (saturation + 0.1) * count
        if score > max_score:
            max_score = score
            dominant_color = (r, g, b)
    return dominant_color


if __name__ == '__main__':
    image_path = r"C:\Users\ZGS\Desktop\herhome\验证appium\images\phone-18607088441.png"
    # (246, 89, 126) 红色
    cut_image(image_path)
